/*
 * @(#)RequestOriginParserDefinition.java 2021年3月10日
 *
 * Copyright (c) 2010 by rayootech.com. All rights reserved.
 */
package com.nuctech.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;

import cn.hutool.core.util.StrUtil;

/**
 * 自定义来源处理规则，适配Sentinel中授权规则中的流控应用名
 * 
 * @author lilu
 * @date 2021年3月10日
 * @since 1.0.0
 */
@Component
public class RequestOriginParserDefinition implements RequestOriginParser {

	/**
	 * 定义区分来源：本质作用是通过request域获取到来源标识，然后交给流控应用位置进行匹配
	 * @see com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser#parseOrigin(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public String parseOrigin(HttpServletRequest request) {
		String serviceName = request.getHeader("Service-Name");
		if (StrUtil.isBlank(serviceName)) {
//			throw new RuntimeException("The \"Service-Name\" parameter in the request header cannot be null");
			serviceName = "";
		}
		return serviceName;
	}

}
